package com.lancoo.ccas52.aspect;

import com.lancoo.ccas52.entity.TeachingClass;
import com.lancoo.ccas52.service.TeachingClassService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;

/**
 * @ClassName CacheRemoveAspect
 * @Description 功能描述:清除缓存切面类
 * @Author quin
 * @Date 2023/2/15 16:40
 */
@Component
@Aspect
@Slf4j
public class CacheRemoveAspect {

    @Resource
    RedisTemplate<String, String> redis;
    @Resource
    private TeachingClassService teachingClassService;

    //截获标有@CacheRemove的方法
    @Pointcut(value = "(@annotation(com.lancoo.ccas52.aspect.CacheRemove))")
    private void pointcut() {
    }

    /**
     * 功能描述: 切面在截获方法返回值之后
     */
    @AfterReturning(value = "pointcut()")
    private void process(JoinPoint joinPoint) {
        //获取被代理的类
        Object target = joinPoint.getTarget();
        //获取切入方法的数据
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入方法
        Method method = signature.getMethod();
        //获得注解
        CacheRemove cacheRemove = method.getAnnotation(CacheRemove.class);

        if (cacheRemove != null) {
            String[] keys = cacheRemove.key();
            Long studentId = (Long) getValueByName(joinPoint, "studentId");
            Long teachingClassId = (Long) getValueByName(joinPoint, "teachingClassId");
            TeachingClass teachingClass = teachingClassService.getById(teachingClassId);
            for (String key : keys) {
                String redisKey;
                switch (key) {
                    case "student:course":
                        redisKey = "ccas52:student:course::" + studentId + "-" + teachingClass.getTeachingClassType();
                        break;
                    case "student:require":
                        redisKey = "ccas52:student:require::" + studentId + "-" + teachingClass.getStudyNature();
                        break;
                    default:
                        return;
                }
                log.info(redisKey);
                redis.delete(redisKey);
            }
//            Object[] paramValues = joinPoint.getArgs();
//            String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
//            for (int i = 0; i < paramNames.length; i++) {
//                if("studentId".equals(paramNames[i])){
//                    Long studentId = (Long)paramValues[i];
//                    teachingClassService.getById()
//                    String[] keys = cacheRemove.key();
//                    for (String key : keys) {
//                        //清除缓存
//                        cleanRedisCache("*" + key + "::"+studentId+"*");
//                    }
//                }
//            }
        }
    }

    private Object getValueByName(JoinPoint joinPoint, String name) {
        Object[] paramValues = joinPoint.getArgs();
        String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
        for (int i = 0; i < paramNames.length; i++) {
            if (name.equals(paramNames[i])) {
                return paramValues[i];
            }
        }
        return null;
    }

    private void cleanRedisCache(String key) {
        Set<String> stringSet = redis.keys(key);
        //删除缓存
        redis.delete(stringSet);
        log.info("清除 " + key + " 缓存");
    }
}

